package com.srv.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.srv.ContentType;
import com.srv.TheException;
import com.srv.model.ID;
import com.srv.model.User;
import com.srv.model.Resource;
import com.srv.persistence.PersistenceFactory;
import com.srv.service.IDService;

/**
 * 获取资源(二进制）
 * @author Administrator
 *
 */
public class ServletRes extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1121312313L;
	private static Log log = LogFactory.getLog(ServletNodeGetid.class);	  
		public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {
	try{
		
		
		ResponseResult result = new ResponseResult();
		User theUser = RequestTool.getAccessUser(request);			
		//未登陆
		if(theUser==null){				
			result.theResult = ResponseResult.RESULT_FAIL;//结果失败
			result.theMessageObj = ConstText.NotLogin;
			ResponseTool.repsone(request,response,result);
			return;
		}
		
		 //参数检查
		 String id = RequestTool.getValueOfID(request);				
		 //检查ID是否合法
		//检查ID是否合法
		 ID idObj = IDService.getID(id);
		 if(idObj==null){
			 result.theResult = ResponseResult.RESULT_FAIL;//结果失败
			 result.theMessageObj=ConstText.ID_IS_INVALID;
			 ResponseTool.repsone(request,response,result);
			 return;		 
		 }
		//参数检查，id为travel的id
		 String name = RequestTool.getValueOfName(request);					
		 //检查Name是否合法
		 if(name==null){
			 result.theResult = ResponseResult.RESULT_FAIL;//结果失败
			 result.theMessageObj=ConstText.PARAMETER_IS_INVALID;
			 ResponseTool.repsone(request,response,result);
			 return;		 
		 }		
		 Resource resource = PersistenceFactory.getResourcePersistence().getResource(id, name);
		 response.setContentType(ContentType.getString(resource.contentType));
		 response.getOutputStream().write(resource.content);
		 response.getOutputStream().flush();
	}catch(TheException te){
		log.error(te.getMessage());
	}catch(Exception e){
		log.error(e.getMessage());
	}
		
	}

}
